package com.wanshi.service;

import com.wanshi.dao.CartDao;
import com.wanshi.dao.OrderDao;
import com.wanshi.dao.ProdDao;
import com.wanshi.entity.*;
import com.wanshi.exception.UserException;
import com.wanshi.utils.JDBCUtil;
import org.apache.commons.dbutils.DbUtils;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

public class OrderService {
    
    private OrderDao orderDao = new OrderDao();
    
    private CartDao cartDao = new CartDao();

    private ProdDao prodDao = new ProdDao();

    public void delOrderById(String orderid) {

        Connection conn = null;
        try{
            conn = JDBCUtil.getConn();
            conn.setAutoCommit(false);

            //3
            //1  订单表删除订单 根据id

            //2 订单项表删除多条数据

            //3 商品加入 库存
            List<OrderItem> items = orderDao.findOrderItemsByOrderId(conn,orderid);
            for(OrderItem item:items){
                prodDao.updateProdNumByIdAdd(conn,item.getProd_id()+"",item.getBuynum());
            }

            //int x = 5/0;

            orderDao.delItemsByOId(conn,orderid);

            orderDao.delOrderById(conn,orderid);

            DbUtils.commitAndCloseQuietly(conn);
        }catch (Exception e){
            DbUtils.rollbackAndCloseQuietly(conn);
            e.printStackTrace();
            throw new UserException("删除失败");
        }

    }

    public void addOrder(Order order) {

        Connection connection = null;
        try{
           connection = JDBCUtil.getConn();
            connection.setAutoCommit(false);
            //1   订单表插入 1条记录
            orderDao.addOrder(connection,order);

            //int x = 5/0;

            //2  订单项表 插入多条记录
            List<CartItem> cartitems = cartDao.findCartItemByUserId(order.getUser_id() + "");
            for(CartItem cartItem : cartitems){
                orderDao.addOrderItem(connection,order.getId()+"",cartItem.getProd_id()+"",cartItem.getBuynum());

                //3 商品   相应商品减去库存
                prodDao.updateProdNumById(connection,cartItem.getProd_id()+"",cartItem.getBuynum());
            }
            //4  购物车表 根据用户id 清除记录
            cartDao.clearCart(connection,order.getUser_id()+"");

            DbUtils.commitAndCloseQuietly(connection);
        }catch (Exception e){
            DbUtils.rollbackAndCloseQuietly(connection);
            e.printStackTrace();
        }
        
    }

    public List<Order> findOrdersByUser(int userid) {
        //用户id

        //1 查询用户订单
        //2 为每一个订单的items赋值
        //3 为每一个item 的product赋值
        List<Order> orders = orderDao.findOrdersByUser(userid);

        for(Order order:orders){
            List<OrderItem> items = orderDao.findOrderItemsByOrderId(order.getId());

            for(OrderItem item:items){
                //
                Product prod = prodDao.findProdById(item.getProd_id() + "");
                item.setProduct(prod);
            }
            order.setItems(items);
        }
        return orders;
    }

    public void updateOrderById(String orderid) {
        orderDao.updateOrderById(orderid);
    }

    public Order findOrdersByOId(String orderid) {
        return orderDao.findOrderByOId(orderid);
    }

    public void updatePayStateByOId(String orderid) {
        orderDao.updatePayStateByOId(orderid);
    }
}
